import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

import routes from "./routes.js";
import util from "@/libs/util";

var router = new VueRouter({ routes });

router.beforeEach((to, from, next) => {
  if (!to.matched.some(r => r.meta.auth)) {
    // 不需要授权
    next();
    return;
  }

  const token = util.cookies.get("token");

  // 关闭了登录验证
  if (process.env.auth_off) {
    util.title(to.meta.title);
    next();
    return;
  }

  // 有登录
  if (token && token !== "undefined") {
    util.title(to.meta.title);
    next();
    return;
  }
  
  const loninRoute = { name: "login", query: { redirect: to.fullPath } };
  // 没有登录的时候跳转到登录界面
  // 携带上登陆成功之后需要跳转的页面完整路径
  next(loninRoute);
});

export default router;
